package com.tsystems.train.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import com.tsystems.train.entity.Station;
import com.tsystems.train.entity.TimeTableElement;
import com.tsystems.train.entity.Train;
import com.tsystems.train.service.StationServices;
import com.tsystems.train.service.TimeTableElementServices;
import com.tsystems.train.service.TrainServices;

@Named("timeTableBean")
@RequestScoped
public class TimeTableBean implements Serializable {

	private static final long serialVersionUID = 2165793273424547096L;

	@EJB
	private TimeTableElementServices tteService;

	@EJB
	private TrainServices trainService;

	@EJB
	private StationServices stationService;

	private TTETableDataModel tteTableDataModel;
	private TimeTableElement selectedTimeTableElement;

	private Train trainSelected;

	private List<Train> trains;

	private List<Station> stations;

	private Station arrivalStation;

	private Station departureStation;

	private Station departureStationFilter;

	private Date arrivalTime;

	private Date departureTime;

	public TimeTableBean() {

		tteTableDataModel = new TTETableDataModel();
		selectedTimeTableElement = new TimeTableElement(new Train(),
				new Station(), new Station(), new Date(), new Date());

		trainSelected = new Train("", 0);
		trains = new ArrayList<Train>();
		stations = new ArrayList<Station>();
		arrivalStation = new Station("");
		departureStation = new Station("");

	}

	@PostConstruct
	public void init() {

		StationConverter.stations = stationService.viewStations();
		stations = StationConverter.stations;
		TrainConverter.trains = trainService.viewTrains();
		trains = TrainConverter.trains;
		updateTable();

	}

	public void updateTable() {
		List<TimeTableElement> result = tteService.viewTimeTableElementsBase();
		tteTableDataModel.setWrappedData(result);

	}

	public String addTTE() {
		TimeTableElement timeTableElement = new TimeTableElement(trainSelected,
				departureStation, arrivalStation, departureTime, arrivalTime);
		String result = tteService.addTimeTableElement(timeTableElement);
		if (result.equals("Element of timetable added")) {
			updateTable();

			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
					"Info", result);

			FacesContext.getCurrentInstance().addMessage(null, message);
		}else{
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
					"Error", result);

			FacesContext.getCurrentInstance().addMessage(null, message);
		}

		return null;
	}

	public TimeTableElement getSelectedTimeTableElement() {
		return selectedTimeTableElement;
	}

	public void setSelectedTimeTableElement(
			TimeTableElement selectedTimeTableElement) {
		this.selectedTimeTableElement = selectedTimeTableElement;
	}

	public TTETableDataModel getTteTableDataModel() {
		return tteTableDataModel;
	}

	public Train getTrainSelected() {
		return trainSelected;
	}

	public void setTrainSelected(Train trainSelected) {
		this.trainSelected = trainSelected;
	}

	public List<Train> getTrains() {
		return trains;
	}

	public void setTrains(List<Train> trains) {
		this.trains = trains;
	}

	public List<Station> getStations() {
		return stations;
	}

	public void setStations(List<Station> stations) {
		this.stations = stations;
	}

	public Station getArrivalStation() {
		return arrivalStation;
	}

	public void setArrivalStation(Station arrivalStation) {
		this.arrivalStation = arrivalStation;
	}

	public Station getDepartureStation() {
		return departureStation;
	}

	public void setDepartureStation(Station departureStation) {
		this.departureStation = departureStation;
	}

	public Date getArrivalTime() {
		return arrivalTime;
	}

	public void setArrivalTime(Date arrivalTime) {
		this.arrivalTime = arrivalTime;
	}

	public Date getDepartureTime() {
		return departureTime;
	}

	public void setDepartureTime(Date departureTime) {
		this.departureTime = departureTime;
	}

	public Station getDepartureStationFilter() {
		return departureStationFilter;
	}

	public void setDepartureStationFilter(Station departureStationFilter) {
		this.departureStationFilter = departureStationFilter;
	}

	public String filterTimeTable() {
		if (departureStationFilter == null) {
			updateTable();
			return null;
		}
		List<TimeTableElement> result = tteService
				.getTimeTable(departureStationFilter);
		tteTableDataModel.setWrappedData(result);
		return null;
	}

}
